# Joy -- implementation of the Joy programming language
# Copyright © 2016, 2017 Eric Bavier <bavier@member.fsf.org>
#
# Joy is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# Joy is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Joy.  If not, see <http://www.gnu.org/licenses/>.

bin_SCRIPTS =					\
  scripts/joy

MODULES =					\
  language/joy/lexer.scm			\
  language/joy/parser.scm			\
  language/joy/compile-tree-il.scm		\
  language/joy/primitives.scm			\
  language/joy/write.scm			\
  language/joy/eval.scm				\
  language/joy/spec.scm				\
  language/joy-repl/spec.scm			\
  joy/ui.scm					\
  $(EMPTY)

JOY_MODULES =					\
  lib/joy/base.joy				\
  lib/joy/forth.joy				\
  $(EMPTY)

joymoduledir = $(pkglibdir)
dist_joymodule_DATA = $(JOY_MODULES)
nobase_dist_guilemodule_DATA = $(MODULES)
nobase_nodist_guilemodule_DATA = joy/config.scm
GOBJECTS = $(MODULES:%.scm=%.go) joy/config.go
nobase_nodist_guileobject_DATA = $(GOBJECTS)

# Flags passed to 'guild compile'.
GUILD_COMPILE_FLAGS =				\
  -Wformat -Wunbound-variable -Warity-mismatch

.scm.go:
	$(AM_V_GUILEC)$(MKDIR_P) `dirname $@`;				\
	unset GUILE_LOAD_COMPILED_PATH ;				\
	$(AM_V_P) && stdout="&1" || stdout="/dev/null";			\
	LC_ALL=C							\
	$(top_builddir)/pre-inst-env					\
	$(GUILD) compile -L "$(top_builddir)" -L "$(top_srcdir)"	\
	  $(GUILD_COMPILE_FLAGS) --target="$(host)"			\
	  -o "$@" "$<" >$$stdout

SUFFIXES = .go

edit = sed \
	-e 's|@GUILE[@]|$(GUILE)|g' \
	-e 's|@prefix[@]|$(prefix)|g' \
	-e 's|@joymoduledir[@]|$(joymoduledir)|g' \
	-e 's|@guilemoduledir[@]|$(guilemoduledir)|g' \
	-e 's|@guileobjectdir[@]|$(guileobjectdir)|g'

scripts/joy: scripts/joy.in Makefile
	$(AM_V_GEN)rm -f $@ $@.tmp
	$(AM_V_at)$(MKDIR_P) $(@D)
	$(AM_V_at)$(edit) $< >$@.tmp
	$(AM_V_at)chmod +x $@.tmp
	$(AM_V_at)chmod a-w $@.tmp
	$(AM_V_at)mv $@.tmp $@

EXTRA_DIST =					\
  .dir-locals.el				\
  bootstrap					\
  guix.scm					\
  scripts/joy.in				\
  $(EMPTY)

CLEANFILES =					\
  $(GOBJECTS)					\
  $(JOY_TESTS:tests/%.scm=%.log)		\
  scripts/joy					\
  $(EMPTY)

DISTCLEANFILES =				\
  $(EMPTY)

include doc.am
include tests.am

AM_V_GUILEC = $(AM_V_GUILEC_$(V))
AM_V_GUILEC_ = $(AM_V_GUILEC_$(AM_DEFAULT_VERBOSITY))
AM_V_GUILEC_0 = @echo "  GUILEC  " $@;
